package com.dhh.activity;

import java.util.Calendar;
import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.CUtil;
import com.dhh.util.Constant;
import com.dhh.util.manager.FoodLogManager;
import com.dhh.util.manager.PlanManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
	int notification_id=19172439;
	private DataBaseHelper db;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();	
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		NotificationManager mn = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		super.onStart(intent, startId);
		if(!checkCalorie()){
			Notification notification=new Notification(R.drawable.history,"GWS Tip",System.currentTimeMillis());
	    	notification.defaults=android.app.Notification.DEFAULT_ALL; 
	    	Intent it = new Intent(this,FoodLogDetailActivity.class);
			String yesterday = CUtil.getDate(Calendar.getInstance().getTime());
	    	it.putExtra(Constant.C_LOG_DATE, yesterday);
			PendingIntent pt = PendingIntent.getActivity(this, 0, it, 0);
			notification.setLatestEventInfo(this,"GWS Notification","Yesterday your intake was not healthy!",pt);
			mn.notify(notification_id, notification);
		}    	
	}
	
	public boolean checkCalorie(){
		boolean flag = false;
		try{
			db = new DataBaseHelper(getBaseContext());
	        db.openDataBase();	        
	        long pc = Long.valueOf(new PlanManager(db).getCurrentPlanSimple().getEnergy());
	        long yc = Long.valueOf(new FoodLogManager(db).getYesterdayEnergy());	        
	        if(yc>=pc) flag = true;	        
	        db.close();
		}catch(Exception e){
			Log.e("GWS",e.getMessage());
		}
		
        return flag;
	}
}
